package cn.fl.infside.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.infside.service.IInfPostCollectDetailService;
import cn.fl.infside.transform.InfPostCollectDetailVOTransform;
import cn.fl.infside.vo.InfPostCollectDetailVO;
import cn.fl.system.constant.SysLogConstant;
import cn.lb.framework.poi.excel.ExcelExportUtil;
import cn.lb.framework.poi.excel.entity.ExportParams;
import cn.lb.framework.poi.excel.entity.enmus.ExcelType;
import cn.lb.framework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
import fl.inf.facade.condition.InfPostCollectDetailQueryCondition;
import fl.inf.facade.constant.InterfaceConstants;
import fl.inf.facade.dto.InfPostCollectDetailDTO;
import fl.inf.facade.dto.InfPostCollectDetailExtDTO;
import fl.inf.facade.dto.MessageDTO;

/**
 * @Description: 邮储代扣明细表 控制层
 */
@Controller
@RequestMapping("/infside/infPostCollectDetail")
public class InfPostCollectDetailController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InfPostCollectDetailController.class);

	/**
	 * InfPostCollectDetail 服务层BEAN
	 */
	@Autowired
	private IInfPostCollectDetailService infPostCollectDetailService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toInfPostCollectDetailList.do", method = { RequestMethod.GET})
	public ModelAndView toInfPostCollectDetailList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("infside/infPostCollectDetail/infPostCollectDetailList");
		List<Map<String,String>> typelist = new ArrayList<Map<String,String>>();
		Map<String,String> typemap1 = new HashMap<String,String>();
		typemap1.put("key","0");
		typemap1.put("value","租金代扣");
		typelist.add(typemap1);
		Map<String,String> typemap2 = new HashMap<String,String>();
		typemap2.put("key","1");
		typemap2.put("value","验证代扣");
		typelist.add(typemap2);
		Map<String,String> typemap3 = new HashMap<String,String>();
		typemap3.put("key","2");
		typemap3.put("value","手动代扣");
		typelist.add(typemap3);
		view.addObject("typelist", typelist);
		BaseLogUtils.info(logger, "toInfPostCollectDetailList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到租金代扣列表页面
	 */
	@RequestMapping(value = "/toRentDetaiList.do", method = { RequestMethod.GET})
	public ModelAndView toRentDetaiList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("infside/infPostCollectDetail/rentDetaiList");
		BaseLogUtils.info(logger, "toRentDetaiList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到验证代扣列表页面
	 */
	@RequestMapping(value = "/toVerifyDetaiList.do", method = { RequestMethod.GET})
	public ModelAndView toVerifyDetaiList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("infside/infPostCollectDetail/verifyDetaiList");
		BaseLogUtils.info(logger, "toVerifyDetaiList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("infside/infPostCollectDetail/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initInfPostCollectDetaiList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initInfPostCollectDetaiList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String accId = request.getParameter("accId");
		String createDateStr = request.getParameter("createDateStr");
		String payDateStr = request.getParameter("payDateStr");
		String sendFileName = request.getParameter("sendFileName");
		String customerName = request.getParameter("customerName");
		String paperId = request.getParameter("paperId");
		String type = request.getParameter("type");
		String masterId = request.getParameter("masterId");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		InfPostCollectDetailQueryCondition qc = new InfPostCollectDetailQueryCondition();
		if (StringUtils.isNotEmpty(accId)) {
			qc.setAccId(accId);
		}
		if (StringUtils.isNotEmpty(createDateStr)) {
			qc.setCreateDateStr(createDateStr);
		}
		if (StringUtils.isNotEmpty(payDateStr)) {
			qc.setPayDateStr(payDateStr);
		}
		if (StringUtils.isNotEmpty(sendFileName)) {
			qc.setSendFileName(sendFileName);
		}
		if (StringUtils.isNotEmpty(customerName)) {
			qc.setCustomerName(customerName);
		}
		if (StringUtils.isNotEmpty(paperId)) {
			qc.setPaperId(paperId);
		}
		if (StringUtils.isNotEmpty(type)) {
			qc.setType(type);
		}
		if (StringUtils.isNotEmpty(masterId)) {
			qc.setMasterId(Long.valueOf(masterId));
		}
		page.setCondition(qc);
		
		//TODO SOMETHING
		Pager pager = this.infPostCollectDetailService.searchListPage(page);
		List<InfPostCollectDetailVO> list = InfPostCollectDetailVOTransform.toVOList((List<InfPostCollectDetailExtDTO>) pager.getResultList());
		for(InfPostCollectDetailVO _vo : list){
			if(null != _vo.getType()){
				Map<String,String> typemap = new HashMap<String,String>();
				typemap.put("0","租金代扣");
				typemap.put("1", "验证代扣");
				typemap.put("2", "手动代扣");
				_vo.setType(typemap.get(_vo.getType()));
			}
			if(null != _vo.getTranAt()){
				_vo.setTranAtdou(DecimalUtils.convertFenToYuan(_vo.getTranAt()));
			}
			if(null != _vo.getReceivableAt()){
				_vo.setReceivableAtdou(DecimalUtils.convertFenToYuan(_vo.getReceivableAt()));
			}
			if(null != _vo.getOverdueAt()){
				_vo.setOverdueAtdou(DecimalUtils.convertFenToYuan(_vo.getOverdueAt()));
			}
			if(null != _vo.getAmount()){
				_vo.setAmountdou(DecimalUtils.convertFenToYuan(_vo.getAmount()));
			}

			if(null != _vo.getAccBalAt()){
				_vo.setAccBalAtdou(DecimalUtils.convertFenToYuan(_vo.getAccBalAt()));
			}

			if(null != _vo.getActAt()){
				_vo.setActAtdou(DecimalUtils.convertFenToYuan(_vo.getActAt()));
			}
			if(StringUtils.isNotEmpty(_vo.getAnsCd())){
				Map<String,String> anscdmap = this.infPostCollectDetailService.searchGroupToMap(InterfaceConstants.SYS_CODE,InterfaceConstants.ORG_CODE, InterfaceConstants.POST_ANS_CD);
				_vo.setAnsCdNm(anscdmap.get(_vo.getAnsCd()));
			}
		}
		ListPageVO<InfPostCollectDetailVO> listPage = new ListPageVO<InfPostCollectDetailVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 初始化租金代扣列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initRentDetaiList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initRentDetaiList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String customerName = request.getParameter("customerName");
		String customerCode = request.getParameter("customerCode");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		InfPostCollectDetailQueryCondition qc = new InfPostCollectDetailQueryCondition();
		if (StringUtils.isNotEmpty(customerName)) {
			qc.setCustomerName(customerName);
		}
		if (StringUtils.isNotEmpty(customerCode)) {
			qc.setCustomerCode(customerCode);
		}
		qc.setType("0");
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = this.infPostCollectDetailService.searchListPage(page);
		List<InfPostCollectDetailVO> list = InfPostCollectDetailVOTransform.toVOList((List<InfPostCollectDetailExtDTO>) pager.getResultList());
		for(InfPostCollectDetailVO _vo : list){
			if(null != _vo.getTranAt()){
				_vo.setTranAtdou(DecimalUtils.convertFenToYuan(_vo.getTranAt()));
			}
			if(null != _vo.getReceivableAt()){
				_vo.setReceivableAtdou(DecimalUtils.convertFenToYuan(_vo.getReceivableAt()));
			}
			if(null != _vo.getOverdueAt()){
				_vo.setOverdueAtdou(DecimalUtils.convertFenToYuan(_vo.getOverdueAt()));
			}
			if(null != _vo.getAmount()){
				_vo.setAmountdou(DecimalUtils.convertFenToYuan(_vo.getAmount()));
			}
		}
		ListPageVO<InfPostCollectDetailVO> listPage = new ListPageVO<InfPostCollectDetailVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initRentDetaiList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	/**
	 * 初始化验证代扣列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initVerifyDetaiList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initVerifyDetaiList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String customerName = request.getParameter("customerName");
		String customerCode = request.getParameter("customerCode");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		InfPostCollectDetailQueryCondition qc = new InfPostCollectDetailQueryCondition();
		if (StringUtils.isNotEmpty(customerName)) {
			qc.setCustomerName(customerName);
		}
		if (StringUtils.isNotEmpty(customerCode)) {
			qc.setCustomerCode(customerCode);
		}
		qc.setType("1");
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = this.infPostCollectDetailService.searchListPage(page);
		List<InfPostCollectDetailVO> list = InfPostCollectDetailVOTransform.toVOList((List<InfPostCollectDetailExtDTO>) pager.getResultList());
		for(InfPostCollectDetailVO _vo : list){
			if(null != _vo.getTranAt()){
				_vo.setTranAtdou(DecimalUtils.convertFenToYuan(_vo.getTranAt()));
			}
		}
		ListPageVO<InfPostCollectDetailVO> listPage = new ListPageVO<InfPostCollectDetailVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initVerifyDetaiList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.infPostCollectDetailService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			InfPostCollectDetailVO vo = this.infPostCollectDetailService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		InfPostCollectDetailVO entity = null;
		try {
			entity = new InfPostCollectDetailVO();
			//TODO ADD PARAM
			Long id = this.infPostCollectDetailService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		InfPostCollectDetailVO entity = null;
		try {
			entity = new InfPostCollectDetailVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.infPostCollectDetailService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 生成租金代扣
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/genRentDeductions.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object genRentDeductions(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String paybegindate = request.getParameter("paybegindate");
		String payenddate = request.getParameter("payenddate");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作完成");
		if(StringUtils.isEmpty(paybegindate) || StringUtils.isEmpty(payenddate)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "genRentDeductions", "操作数据,参数为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			if(StringUtils.isNotEmpty(paybegindate)){
				paybegindate = paybegindate+" 00:00:00";
			}
			if(StringUtils.isNotEmpty(payenddate)){
				payenddate = payenddate+" 23:59:59";
			}
			MessageDTO<String> messagedto = this.infPostCollectDetailService.genRentDeductions(DateUtils.formatStringToDate(paybegindate, DateUtils.YYYY_MM_DDHHMMSS),DateUtils.formatStringToDate(payenddate, DateUtils.YYYY_MM_DDHHMMSS));
			if(!messagedto.isSuccess()){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				//result.setInfo(messagedto.getMessage());
				result.setInfo("操作失败");
				BaseLogUtils.info(logger, "genRentDeductions", "操作失败", CurrentThreadContext.getCurrentUserName());
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("操作失败！");
			logger.error("操作失败！", ex);
		}
		return result;
	}

	/**
	 * 生成验证代扣
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	@RequestMapping(value = "/genVerifyDeductions.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object genVerifyDeductions(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String paybegindate = request.getParameter("paybegindate");
		String payenddate = request.getParameter("payenddate");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作完成");
		if(StringUtils.isEmpty(paybegindate) || StringUtils.isEmpty(payenddate)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "genVerifyDeductions", "操作数据,参数为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			if(StringUtils.isNotEmpty(paybegindate)){
				paybegindate = paybegindate+" 00:00:00";
			}
			if(StringUtils.isNotEmpty(payenddate)){
				payenddate = payenddate+" 23:59:59";
			}
			MessageDTO<String> messagedto = this.infPostCollectDetailService.genVerifyDeductions(DateUtils.formatStringToDate(paybegindate, DateUtils.YYYY_MM_DDHHMMSS),DateUtils.formatStringToDate(payenddate, DateUtils.YYYY_MM_DDHHMMSS));
			if(!messagedto.isSuccess()){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				//result.setInfo(messagedto.getMessage());
				result.setInfo("操作失败");
				BaseLogUtils.info(logger, "genRentDeductions", "操作失败", CurrentThreadContext.getCurrentUserName());
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("操作失败！");
			logger.error("操作失败！", ex);
		}
		return result;
	}

	/**
	 * 导出
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/exportCollectDetailExcel.do", method = {RequestMethod.POST, RequestMethod.GET})
	public void exportCollectDetailExcel(HttpServletRequest request, HttpServletResponse response) {
		String accId = request.getParameter("accId");
		String createDateStr = request.getParameter("createDateStr");
		String payDateStr = request.getParameter("payDateStr");
		String sendFileName = request.getParameter("sendFileName");
		String customerName = request.getParameter("customerName");
		String paperId = request.getParameter("paperId");
		String type = request.getParameter("type");
		String masterId = request.getParameter("masterId");

		InfPostCollectDetailQueryCondition qc = new InfPostCollectDetailQueryCondition();
		if (StringUtils.isNotEmpty(accId)) {
			qc.setAccId(accId);
		}
		if (StringUtils.isNotEmpty(createDateStr)) {
			qc.setCreateDateStr(createDateStr);
		}
		if (StringUtils.isNotEmpty(payDateStr)) {
			qc.setPayDateStr(payDateStr);
		}
		if (StringUtils.isNotEmpty(sendFileName)) {
			qc.setSendFileName(sendFileName);
		}
		if (StringUtils.isNotEmpty(customerName)) {
			qc.setCustomerName(customerName);
		}
		if (StringUtils.isNotEmpty(paperId)) {
			qc.setPaperId(paperId);
		}
		if (StringUtils.isNotEmpty(type)) {
			qc.setType(type);
		}
		if (StringUtils.isNotEmpty(masterId)) {
			qc.setMasterId(Long.valueOf(masterId));
		}
		List<InfPostCollectDetailVO> list = this.infPostCollectDetailService.searchList(qc);
		for(InfPostCollectDetailVO _vo : list){
			if(null != _vo.getType()){
				Map<String,String> typemap = new HashMap<String,String>();
				typemap.put("0","租金代扣");
				typemap.put("1", "验证代扣");
				typemap.put("2","手动代扣");
				_vo.setType(typemap.get(_vo.getType()));
			}
			if(null != _vo.getTranAt()){
				_vo.setTranAtdou(DecimalUtils.convertFenToYuan(_vo.getTranAt()));
			}
			if(null != _vo.getReceivableAt()){
				_vo.setReceivableAtdou(DecimalUtils.convertFenToYuan(_vo.getReceivableAt()));
			}
			if(null != _vo.getOverdueAt()){
				_vo.setOverdueAtdou(DecimalUtils.convertFenToYuan(_vo.getOverdueAt()));
			}
			if(null != _vo.getAmount()){
				_vo.setAmountdou(DecimalUtils.convertFenToYuan(_vo.getAmount()));
			}
			if(null != _vo.getAnsCd()){
				Map<String,String> anscdmap = this.infPostCollectDetailService.searchGroupToMap(InterfaceConstants.SYS_CODE,InterfaceConstants.ORG_CODE, InterfaceConstants.POST_ANS_CD);
				_vo.setAnsCdNm(anscdmap.get(_vo.getAnsCd()));
			}
			if(null != _vo.getAccBalAt()){
				_vo.setAccBalAtdou(DecimalUtils.convertFenToYuan(_vo.getAccBalAt()));
			}
			if(null != _vo.getActAt()){
				_vo.setActAtdou(DecimalUtils.convertFenToYuan(_vo.getActAt()));
			}
		}
		ExportParams params = new ExportParams("代扣明细", "代扣明细", ExcelType.HSSF);
		//HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现;
		//XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现
		//SXSSF基于XSSF的低内存占用的API
		params.setColor(HSSFColor.BRIGHT_GREEN.index);//表头颜色
		params.setStyle(ExcelExportStylerDefaultImpl.class);//ExcelExportStylerBorderImpl 有边框样式 ExcelExportStylerColorImpl  有边框间隔行样式 ExcelExportStylerDefaultImpl	默认的样式
		Workbook workbook = ExcelExportUtil.exportExcel(params, InfPostCollectDetailVO.class, list);
		OutputStream ouputStream = null;
		try {
			String filename="代扣明细";
			filename=new String(filename.getBytes("UTF-8"),"ISO8859-1");
			response.reset();
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");// 设定输出文件头
			response.setContentType("application/x-msdownload");// 定义输出类型
			ouputStream = response.getOutputStream();
			workbook.write(ouputStream);
			ouputStream.flush();
			ouputStream.close();
		} catch (UnsupportedEncodingException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
		} catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
		}finally {
			//关闭流
			if(ouputStream!=null){
				try {
					ouputStream.close();
				} catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
				}
			}
		}

	}

	/**
	 * 手动代扣解析excel
	 */
	@RequestMapping(value = "/manualDetaiXls.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object manualDetaiXls(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作完成");
		String filePath = request.getParameter("filePath");
		if(StringUtils.isEmpty(filePath)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("解析文件错误");
			BaseLogUtils.info(logger, "manualDetaiXls", "操作数据,解析文件路径为空!", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		try {
			//同时支持Excel 2003、2007
			File excelFile=new File(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH)+filePath);//创建文件对象
			FileInputStream is = new FileInputStream(excelFile); //文件流
			Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
			Sheet sheet = workbook.getSheetAt(0);
			int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
			List<InfPostCollectDetailDTO> dtolist = new ArrayList<InfPostCollectDetailDTO>();
			for (int r = 1; r < rowCount; r++) { // 从第二行开始，去除第一行标题
				InfPostCollectDetailDTO _dto = new InfPostCollectDetailDTO();
				Row row = sheet.getRow(r);
				Cell cell = row.getCell(0);//用户号（卡号）
				Cell cell1 = row.getCell(1);//客户名称
				Cell cell2 = row.getCell(2);//证件号
				Cell cell3 = row.getCell(3);//交易总金额
				String accId = this.analyticalCell(cell);
				String accName = this.analyticalCell(cell1);
				String paperId = this.analyticalCell(cell2);
				String tranAt = this.analyticalCell(cell3);

				if(StringUtils.isNotEmpty(accId)){
					_dto.setAccId(accId);
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("用户号（卡号）不能为空");
					return result;
				}
				if(StringUtils.isNotEmpty(accName)){
					_dto.setAccName(accName);
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("客户名称不能为空");
					return result;
				}
				if(StringUtils.isNotEmpty(paperId)){
					_dto.setPaperId(paperId);
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("证件号不能为空");
					return result;
				}
				if(StringUtils.isNotEmpty(tranAt)){
					Double tranAtdou = Double.valueOf(tranAt);
					_dto.setTranAt(DecimalUtils.convertYuanToFen(tranAtdou));
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("证件号不能为空");
					return result;
				}
				dtolist.add(_dto);
			}
			MessageDTO<String> messagedto = this.infPostCollectDetailService.genManualDeductions(dtolist);
			if (!messagedto.isSuccess()) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				//result.setInfo(messagedto.getMessage());
				result.setInfo("操作失败");
				BaseLogUtils.info(logger, "manualDetaiXls", "操作失败", CurrentThreadContext.getCurrentUserName());
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "manualDetaiXls", "解析失败.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("解析错误");
			BaseLogUtils.info(logger, "manualDetaiXls", "解析失败,系统错误.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName(),ex);
		}

		return result;
	}

	/**
	 * 解析cell
	 * @param cell
	 * @return
	 */
	private String analyticalCell(Cell cell){
		if(null == cell) return "";
		int cellType = cell.getCellType();
		String cellValue = "";
		switch(cellType) {
			case Cell.CELL_TYPE_STRING: //文本
				cellValue = cell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_NUMERIC: //数字、日期
				double sss = cell.getNumericCellValue();
				DecimalFormat df = new DecimalFormat("#");
				cellValue = df.format(sss); //数字
				break;
			case Cell.CELL_TYPE_BLANK: //空白
				cellValue = cell.getStringCellValue();
				break;
			default:
				cellValue = "";
		}
		return cellValue;
	}
}

